package com.tiantian.homework_5_login.service.impl;


import com.tiantian.homework_5_login.mapper.UserMapper;
import com.tiantian.homework_5_login.pojo.Result;
import com.tiantian.homework_5_login.pojo.User;
import com.tiantian.homework_5_login.service.UserService;
import com.tiantian.homework_5_login.utils.Base64Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result login(User user) {
        // 数据加密与数据库账号密码做对比
        user.setUsername(Base64Util.base64(user.getUsername()));
        user.setPassword(Base64Util.base64(user.getPassword()));
        int count = userMapper.selectUserByUsernameAndPassword(user);
        String token = UUID.randomUUID().toString();
        if (count > 0) {
            // 使用uuid模拟token
            return Result.ok("登录成功!",token);
        }
        return Result.error("账号或者密码错误!");
    }
}
